export class Subscribe {
    subMap: Map<string, {
        user: string,
        fn: (param: any) => void
    }[]>

    constructor() {
      // 维护注册列表
      this.subMap = new Map();
    }
    // 添加监听者
    on<T>(name: string, user: string, fn: (param: T) => void) {
      const getSub = this.subMap.get(name);
      this.subMap.set(name, getSub ? getSub.concat({
        user,
        fn
      }) : [{
        user,
        fn
      }]);
    }
    // 移除监听
    off(name: string, user: string) {
      const getSub = this.subMap.get(name);

      if (getSub) {
        getSub.forEach((item, index) => {
          item.user === user && getSub.splice(index, 1);
        });
        this.subMap.set(name, getSub);
      }
    }
    // 触发
    emit<T>(name: string, params: T) {
      // 触发事件
      const getSub = this.subMap.get(name);
      getSub && getSub.forEach((item) => {
        item.fn(params);
      });
    }
}
